package com.cskaoyan._01btyestream.in;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/17 16:29
 **/
/*
循环读取数据
 */
public class Demo3 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("a.txt");

        // 普通方法
        // readWhile1(in);

        // 单字节读取
        // readWhile2(in);


        // 批量读取
        int readCount;
        byte[] bytes = new byte[1024];
        while ((readCount = in.read(bytes)) != -1) {
            System.out.println(new String(bytes,0,readCount));
        }


        // close
        in.close();
    }

    private static void readWhile2(FileInputStream in) throws IOException {
        int readData;
        while ((readData = in.read()) != -1) {
            System.out.print(((char) readData));
        }
    }

    private static void readWhile1(FileInputStream in) throws IOException {
        while (true) {
            int readData = in.read();
            if (readData == -1) {
                break;
            }
            System.out.print(((char) readData));
        }
    }
}
